package cn.itcast.order.service;


import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import cn.itcast.user.pojo.User;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    //所需bean不在默认扫描范围，(required = false)，或再启动类App注解指定
//    @Autowired
//    private UserClient userClient;
//
//    public Order queryOrderById(Long orderId) {
//        // 1.查询订单
//        Order order = orderMapper.findById(orderId);
//        //2.用Feign
//        User user=userClient.findById(order.getUserId());
//        //3.封装user到order
//        order.setUser(user);
////        // 4.返回
//        return order;
//    }

    @Autowired
    private RestTemplate restTemplate;

    public Order queryOrderById(Long orderId) {

        // 1.查询订单，在订单数据库中查，只能查到用户ID，并没有用户的详细信息，order的user属性为空，
        Order order = orderMapper.findById(orderId);
        //2.利用RestTemplate发起HTTP请求，用户数据库接收到请求，根据用户id查询用户详细信息并返回
        String url="http://userservice/user/"+order.getUserId();
        User user= restTemplate.getForObject(url, User.class);//从url处获取json对象，并封装成user类型，
        //3.封装user到order
        order.setUser(user);
        // 4.返回
        return order;
    }
}
